package com.cgbl.cg.vm.project

import android.app.Application
import androidx.databinding.ObservableField
import api.ProjectServiceFactory
import com.blankj.utilcode.util.StringUtils
import com.cgbl.cg.R
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.util.ToastUtils
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ReleaseProjectViewModel(application: Application) : GLBaseViewModel(application) {
    val promptSelect = ObservableField(false)
    val typeText = ObservableField<String>()
    val list = mutableListOf<CourseAllTypeBean>()
    val uc = UIChangeEvent()

    val typeId = ObservableField("")
    val name = ObservableField("")
    val info = ObservableField("")
    val budget = ObservableField("")
    val cycle = ObservableField("")
    val phone = ObservableField("")

    class UIChangeEvent {
        val showTypeDialog = SingleLiveEvent<Void>()

    }

    val confirmCommand = BindingCommand<Any>(BindingAction {
        createConsts()
    })

    val typeCommand = BindingCommand<Any>(BindingAction {
        uc.showTypeDialog.call()
    })

    val promptClick = BindingCommand<Any>(BindingAction {
        promptSelect.set(!promptSelect.get()!!)
    })

    private fun checkPrompt(): Boolean {
        if (promptSelect.get() == false) {
            ToastUtils.showShort(StringUtils.getString(R.string.app_error_4))
        }
        return promptSelect.get() == false
    }

    fun allType() {
        ProjectServiceFactory.allType()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
                override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                    list.addAll(t.data)
                }
            })
    }

    fun createConsts(){
        ProjectServiceFactory.createConsts(
            mapOf(
                "itemName" to name.get(),
                "itemBudget" to budget.get(),
                "itemCycle" to cycle.get(),
                "itemInfo" to info.get(),
                "itemPhone" to phone.get(),
                "itemTypeId" to typeId.get()
            )
        ).subscribe(object :AbstractViewModelSubscriber<BaseResponse<String>>(this){
            override fun onNext(t: BaseResponse<String>) {
                ToastUtils.showShort("成功")
                finish()
            }
        })
    }

}